package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.nio.channels.ClosedSelectorException;

/**
 * 使用反射机制操作构造器实例化对象
 */
public class reflectDemo3 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Person p=new Person();
        System.out.println(p);
                                //报名.类名
        Class cls= Class.forName("reflect.Person");
        Constructor c=cls.getConstructor();//获取无参构造器
        Object o=c.newInstance();
        System.out.println(o);

        Constructor c2=cls.getConstructor(String.class,int.class);
        Object o2=c2.newInstance("王二麻子",53);
        System.out.println(o2);

        Constructor c3=cls.getConstructor(String.class);
        Object o3=c3.newInstance("李老六");
        System.out.println(o3);


    }
}
